<?php
if( !defined('CORE') ) exit('Request Error!');
/**
 * 首页管理
 * @author yangzhi
 *
 */
class mod_pf_homepage
{
    //状态
	public static $status = array(0=>'未审核',1=>'审核通过',2=>'审核失败');
	public static $status_name = array(0=>'未审核', 1=>'审核通过', 2=>'审核失败');
	public static $pf_name = array(0=>'安卓', 1=>'苹果', 2=>'全部');

    /**
     * 微信banner列表
     */
    public static function wechat_banner()
    {
        $where = $url = array();

        $view_start_time = req::item("view_start_time", '');
        tpl::assign("view_start_time", $view_start_time);
        if($view_start_time)
        {
            $where[] = "`view_start_time` >= '{$view_start_time}'";
            $url['view_start_time'] = $view_start_time;
        }

        $view_end_time = req::item("view_end_time", '');
        tpl::assign("view_end_time", $view_end_time);
        if($view_end_time)
        {
            $where[] = "`view_end_time` <= '{$view_end_time}'";
            $url['view_end_time'] = $view_end_time;
        }
        $where = empty($where) ? '' : 'WHERE ' . implode(' AND ', $where);
        $url = empty($url) ? '' : '&' . http_build_query($url);
        $order = "";
        $current_page = req::item('page_no', 1);
        $page_size = 50;
        $offset = ($current_page - 1) * $page_size;

        // 高效分页sql
        $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM `wechat_box_banner` {$where} {$order} LIMIT {$offset}, {$page_size}";
        $list = db::get_all($sql);
        $row = db::get_one("SELECT FOUND_ROWS() AS rowscount");
        $config['current_page'] = $current_page; // 当前页数,至少为1
        $config['page_size'] = $page_size; // 每页显示多少条
        $config['total_rs'] = $row['rowscount']; // 总记录数
        $config['url_prefix'] = '?ct=pf_homepage&ac=wechat_banner' . $url; // 网址前缀
        $config['page_name'] = 'page_no'; // 当前分页变量名(默认是page_no,即访问是用 url_prefix&page_no=xxx )
        $config['move_size'] = 2; // 前后偏移量（默认是5）
        $config['input'] = 0; // 是否使用输入跳转框(0|1)
        $pages = util::pagination($config);

        if($list)
        {

            foreach($list as $k => $v)
            {
                $list[$k]['status'] = self::$status_name[$v['status']];
                $list[$k]['platform'] = self::$pf_name[$v['platform']];
            }
        }

        tpl::assign('list', $list);
        tpl::assign('url', $url);
        tpl::assign('pages', $pages);
        tpl::display('homepage.wechat_banner.index.tpl');
    }

    /**
     * 微信banner新增
     */
    public static function wechat_banner_add()
    {
        if (req::$posts)
        {
            $data['img_url'] = mod_upload::move_img(req::post('img_url', '')) ? req::post('img_url', '') : '';
            $data['go_url'] = req::post('go_url', '');
            $data['sort'] = req::post('sort', 0, 'int');
            $data['view_start_time'] = req::post('view_start_time', '', 'text');
            $data['view_end_time'] = req::post('view_end_time', '', 'text');
            $data['platform'] = req::post('platform', 0, 'int');
            $data['status'] = req::post('status', 0, 'int');
            db::insert('wechat_box_banner', $data);
            $gourl = req::item('gourl');
            cls_access::show_message ('微信benner管理', '新增成功', $gourl);
        }
        else
        {
            $gourl = empty($_SERVER['HTTP_REFERER']) ? '?ct=pf_homepage&ac=wechat_banner' : $_SERVER['HTTP_REFERER'];
            tpl::assign('status_name', self::$status_name);
            tpl::assign('pf_name', self::$pf_name);
            tpl::assign('gourl', $gourl);
            tpl::display('homepage.wechat_banner.add.tpl');
        }
    }

    /**
     * 微信banner编辑
     */
    public static function wechat_banner_edit()
    {
        $id = req::item('id', 0, 'int');
        if (req::$posts)
        {
            if(mod_upload::move_img(req::post('img_url', '')))//修改图片才更新
            {
                $data['img_url'] = req::post('img_url', '');
            }
            $data['go_url'] = req::post('go_url', '');
            $data['sort'] = req::post('sort', 0, 'int');
            $data['view_start_time'] = req::post('view_start_time', '', 'text');
            $data['view_end_time'] = req::post('view_end_time', '', 'text');
            $data['platform'] = req::post('platform', 0, 'int');
            $data['status'] = req::post('status', 0, 'int');
            db::update('wechat_box_banner', $data, "`id`={$id}");
            $gourl = req::item('gourl');
            cls_access::show_message ('微信benner管理', '修改成功', $gourl);
        }
        else
        {
            $sql = "SELECT * FROM `wechat_box_banner` WHERE `id`='{$id}'";
            $banner = db::get_one($sql);
            tpl::assign('v', $banner);
            $gourl = empty($_SERVER['HTTP_REFERER']) ? '?ct=pf_homepage&ac=wechat_banner' : $_SERVER['HTTP_REFERER'];
            tpl::assign('status_name', self::$status_name);
            tpl::assign('pf_name', self::$pf_name);
            tpl::assign('gourl', $gourl);
            tpl::display('homepage.wechat_banner.edit.tpl');
        }
    }

    /**
     * 微信banner删除
     */
    public static function wechat_banner_del()
    {
        $id = req::item('id', 0, 'int');
        db::query("DELETE FROM `wechat_box_banner` WHERE `id` = {$id}");
        $gourl = '?ct=pf_homepage&ac=wechat_banner';
        cls_access::show_message ('微信benner管理', '删除成功', $gourl);
    }



}


